/**
 *
 */
package persistence;

import interfaces.DataRepository;
import java.io.File;
import java.io.IOException;

import server.ForumServer;

import domain.items.MetaData;
import domain.items.Topic;
import domain.items.ForumThread;
import domain.items.Post;

/**
 * @author doralt
 *
 */
public class XMLDataRepository implements DataRepository {


	private static XMLDataRepository XMLData = null;

	/**
	 * the name of the XML file where data will be stored
	 */
	private final File DATA_FILE;

	//private final File META_DATA_FILE = new File("metaData.xml");

	/**
	 * root Topic, the most upper node of the forum
	 */
	private Topic _root = null;


	/**
	 *
	 */
	private XMLFileHandler _filerHandler;

	public XMLDataRepository(String filename) throws IOException {
		//the constructors are the same in XMLDataRepository & XMLUserRepository - maybe we should
		// combine them and put them in a single method in XMLUtils. the only diffrence is the casting...
		this._filerHandler = new XMLFileHandler();
		this.DATA_FILE = new File(filename);
		Object tRoot = this._filerHandler.loadDB(DATA_FILE);
		if (tRoot != null){
			this._root = (Topic)tRoot;
		}
		else{
			this._root = new Topic(null);
		}
		// this is to setup all the forum datbase
		// WARNING!! DELETES ALL THREADS AND ALL POSTS
		/*this._root = new Topic(null);
		Topic node1 = new Topic(this._root);
		node1.setName("Year 1");
		this._root.getSubItems().add(node1);
		Topic node2 = new Topic(this._root);
		node2.setName("Year 2");
		this._root.getSubItems().add(node2);
		Topic node3 = new Topic(this._root);
		node3.setName("Year 3");
		this._root.getSubItems().add(node3);
		Topic node4 = new Topic(this._root);
		node4.setName("Year 4");
		this._root.getSubItems().add(node4);
		this._filerHandler.saveDB(this._root,this.DATA_FILE);*/
	}


	/**
	 *
	 * @see interfaces.DataRepository#getRootTopic()
	 */
	public synchronized Topic getRootTopic() {

		return this._root;
	}

	/**
	 *
	 *
	 */
	public boolean addPost(Post post) {
		ForumThread tthread = (ForumThread) post.getFather();
		try {
			tthread.getPosts().add(post);
			return this.update();
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 *
	 */
	public boolean addThread(ForumThread newThread) {
		Topic ttopic = (Topic)newThread.getFather();
		try {
			ttopic.getSubItems().add(newThread);
			return this.update();
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 *
	 */
	public boolean addTopic(Topic newTopic) {
		Topic ttopic = (Topic) newTopic.getFather();
		try {
			ttopic.getSubItems().add(newTopic);
			return this.update();
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 *
	 */
	public boolean updatePost(Post post){
		try {
			return this.update();
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * @throws IOException
	 *
	 */
	private boolean update() throws IOException{
		this._filerHandler.saveDB(this._root, this.DATA_FILE);
		//////////////// SHAI /////////////////
		XMLUtils.saveDB(ForumServer._metaData, MetaData.FILE_NAME);
		//////////////////////////////////////
		return true;
	}


	public boolean deletePost(Post post) {
		ForumThread tthread = (ForumThread)post.getFather();
		tthread.getPosts().removeElement(post);
		try {
			this.update();
		} catch (IOException e) {
			System.err.println("A Critical Error with Updating of A Post!!!");
			return false;
		}
		post = null;
		return true;
	}


	/**
	 * public method that will be invoked from other classes
	 * @param filename - the filename where data is stored
	 * @return the singleton XMLDataRepository
	 * @throws IOException
	 */
	public static DataRepository getInstance(String filename) throws IOException{
		if(XMLData == null) {
			synchronized (XMLDataRepository.class) {
				if(XMLData == null) {
					XMLData = new XMLDataRepository(filename);
				}
			}
		}
		return XMLData;
	}


	public void generateStat(int i, String name) {
		System.out.println("Statisics not supported in XML mode!");
	}


	public void updateUserLoginStat() {
		System.out.println("Statisics not supported in XML mode!");
	}


}
